###############################
### LOAD PACKAGES AND SCRIPTS
###############################

library(lavaan)
library(effects)
library(psych)
library(multicon)

##############################
### LOAD AND PREPARE DATA
##############################

# IMPORTANT!!! set working directory here using setwd command #

data <- read.csv("third pilot - cognitive abilities test - data.csv", header=TRUE, stringsAsFactors = FALSE)

## coding for Raven test of cognitive intelligence #### 
data$raven1<- NA; data$raven1[data$Raven1==7] <- 1; data$raven1[data$Raven1!=7] <- 0
data$raven2<- NA; data$raven2[data$Raven2==4] <- 1; data$raven2[data$Raven2!=4] <- 0
data$raven3<- NA; data$raven3[data$Raven3==6] <- 1; data$raven3[data$Raven3!=6] <- 0
data$raven4<- NA; data$raven4[data$Raven4==2] <- 1; data$raven4[data$Raven4!=2] <- 0
data$raven5<- NA; data$raven5[data$Raven5==4] <- 1; data$raven5[data$Raven5!=4] <- 0
data$raven6<- NA; data$raven6[data$Raven6==7] <- 1; data$raven6[data$Raven6!=7] <- 0
data$raven7<- NA; data$raven7[data$Raven7==3] <- 1; data$raven7[data$Raven7!=3] <- 0
data$raven8<- NA; data$raven8[data$Raven8==8] <- 1; data$raven8[data$Raven8!=8] <- 0
data$raven9<- NA; data$raven9[data$Raven9==7] <- 1; data$raven9[data$Raven9!=7] <- 0
data$raven10<- NA; data$raven10[data$Raven10==3] <- 1; data$raven10[data$Raven10!=3] <- 0
data$raven11<- NA; data$raven11[data$Raven11==7] <- 1; data$raven11[data$Raven11!=7] <- 0
data$raven12<- NA; data$raven12[data$Raven12==5] <- 1; data$raven12[data$Raven12!=5] <- 0
data$raven13<- NA; data$raven13[data$Raven13==5] <- 1; data$raven13[data$Raven13!=5] <- 0
data$raven14<- NA; data$raven14[data$Raven14==4] <- 1; data$raven14[data$Raven14!=4] <- 0
data$raven15<- NA; data$raven15[data$Raven15==1] <- 1; data$raven15[data$Raven15!=1] <- 0

# count number of items attempted #
data$item1attempted <- NA; data$item1attempted[data$Raven1>0] <- 1;
data$item2attempted <- NA; data$item2attempted[data$Raven2>0] <- 1;
data$item3attempted <- NA; data$item3attempted[data$Raven3>0] <- 1;
data$item4attempted <- NA; data$item4attempted[data$Raven4>0] <- 1;
data$item5attempted <- NA; data$item5attempted[data$Raven5>0] <- 1;
data$item6attempted <- NA; data$item6attempted[data$Raven6>0] <- 1;
data$item7attempted <- NA; data$item7attempted[data$Raven7>0] <- 1;
data$item8attempted <- NA; data$item8attempted[data$Raven8>0] <- 1;
data$item9attempted <- NA; data$item9attempted[data$Raven9>0] <- 1;
data$item10attempted <- NA; data$item10attempted[data$Raven10>0] <- 1;
data$item11attempted <- NA; data$item11attempted[data$Raven11>0] <- 1;
data$item12attempted <- NA; data$item12attempted[data$Raven12>0] <- 1;
data$item13attempted <- NA; data$item13attempted[data$Raven13>0] <- 1;
data$item14attempted <- NA; data$item14attempted[data$Raven14>0] <- 1;
data$item15attempted <- NA; data$item15attempted[data$Raven15>0] <- 1;

with(data, describe(cbind(item1attempted,item2attempted,item3attempted,item4attempted,item5attempted,item6attempted,item7attempted,item8attempted,item9attempted,item10attempted,item11attempted,item12attempted,item13attempted,item14attempted,item15attempted)))

# create a table displaying how many items people attempted #
data$raventotalattempted<- with(data, (rowSums(cbind(item1attempted,item2attempted,item3attempted,item4attempted,item5attempted,item6attempted,item7attempted,item8attempted,item9attempted,item10attempted,item11attempted,item12attempted,item13attempted,item14attempted,item15attempted), na.rm=TRUE)))
table(data$raventotalattempted)

# calculate total raven total score and percent #
data$raventotal<- with(data, (rowSums(cbind(raven1,raven2,raven3,raven4,raven5,raven6,raven7,raven8,raven9,raven10,raven11,raven12,raven13,raven14,raven15), na.rm=TRUE)))
data$ravenpercent <- data$raventotal/15
with(data, describe(cbind(raventotal, ravenpercent,raventotalattempted)))

# calculate split half reliability #
raven.rel <- with(data, data.frame(cbind(raven1,raven2,raven3,raven4,raven5,raven6,raven7,raven8,raven9,raven10,raven11,raven12,raven13, raven14, raven15)))
splithalf.r(raven.rel, sims = 1000, graph = TRUE)

# convert self rating from factor to NUMERIC #
data$ravenselfrating<- as.numeric(data$ravenselfrating)

# convert self rating to percentage #
data$ravenselfratingpercent <- data$ravenselfrating/15

# descriptives of self rating and ravens test #
with(data, describe(cbind(ravenselfrating,ravenselfratingpercent,raventotal,ravenpercent)))

# correlations of raven and selfrating #
with(data, corr.test(cbind(ravenselfrating,ravenselfratingpercent,raventotal,ravenpercent)))

## evidence for matches and mismatches ##

# calculate standardized scores (z-scores) for each participant 
data$z.raven <- (data$raventotal-7.5)/(sd(data$raventotal,na.rm=TRUE))
data$z.ravenSV <- (data$ravenselfrating-7.5)/(sd(data$ravenselfrating,na.rm=TRUE))
with(data,describe(cbind(z.raven, z.ravenSV)))

# calculate the people who had accurate self-insight within half a SD#
data$overestimater <- NA
data$overestimater[(data$z.raven)<(data$z.ravenSV-.5)] <- 1
data$overestimater[(data$z.raven)>=(data$z.ravenSV-.5)] <- 0
data$underestimater <- NA
data$underestimater[(data$z.raven)>(data$z.ravenSV+.5)] <- 1
data$underestimater[(data$z.raven)<=(data$z.ravenSV+.5)] <- 0
data$accurate <- NA; 
data$accurate[(data$z.raven)>=(data$z.ravenSV-.5) & (data$z.raven)<=(data$z.ravenSV+.5)] <- 1
data$accurate[(data$z.raven)<(data$z.ravenSV-.5) | (data$z.raven)>(data$z.ravenSV+.5)] <- 0

# display the people who were over- or under- or correct estimaters #
xtabs(~ overestimater, data = data)
xtabs(~ underestimater, data = data)
xtabs(~ accurate, data = data)

# 48 overestimators, 11 under, 39 accurate -- 98 total
